package com.itheima.web.listener;

import com.itheima.autoconfig.green.AliyunGreenTemplate;
import com.itheima.domain.db.Log;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.LogService;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component //监听rabbitMQ中的消息 一但有消息 读取出来 消费即可
public class LogListener {

    @DubboReference
    private LogService logService;
    //监听
    @RabbitListener(queuesToDeclare =@Queue("tanhua.log"))
    public void listenerLog(Log log){
        System.out.println("======>>>>>>>>>>>>获得到了消息:" + log);
        //就可以在此处进行保存操作
        logService.save(log);
    }

    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;
    @DubboReference
    private MovementService movementService;
    //监听
    @RabbitListener(queuesToDeclare =@Queue("tanhua.movement.state"))
    public void listenerMovementState(String movementId){
        System.out.println("======>>>>>>>>>>>>需要检测数据:" + movementId);
        //根据动态的id 查询动态数据
        Movement movement = movementService.findById(new ObjectId(movementId));
        //文本的检测
        Boolean flag1 = aliyunGreenTemplate.checkText(movement.getTextContent());
        //图片检测
        Boolean flag2 = aliyunGreenTemplate.checkImage(movement.getMedias());
        System.out.println("------->>>"+flag1);
        System.out.println("------->>>"+flag2);
        if(flag1){ //都是正确的前提下 才修改状态值  不校验图片
            movement.setState(1);//修改状态值
            movementService.updateMovement(movement);
        }
    }

}
